Git is a distributed version control system used by developers to track changes in their code, collaborate with teams, and manage different versions of a project efficiently. Whether you are a beginner or an experienced developer, knowing essential Git commands is crucial for improving workflow and productivity.
In this guide, we will cover all essential Git commands, their usage, practical examples, and common mistakes to avoid.
Git is a version control system that allows multiple developers to work on the same project without conflicts. It enables:
✅ Tracking changes in the codebase
✅ Collaborating with team members
✅ Maintaining different versions of the project
✅ Rolling back changes if needed
Let’s explore Git commands that every developer should know!
Basic Git commands help you initialize repositories, track changes, and commit updates.
Command | Description | Example | Explanation |
---|---|---|---|
git init | Initializes a new Git repository | git init my-project | Creates a .git folder to start version control. |
git clone <repository_url> | Clones an existing repository | git clone https://github.com/user/repo.git | Copies a remote repository to your local machine. |
git add <file> | Adds a file to the staging area | git add index.html | Prepares a file for commit. |
git commit -m "message" | Saves changes to the repository | git commit -m "Initial commit" | Creates a snapshot of changes with a message. |
git status | Shows the current status of the working directory | git status | Displays modified and untracked files. |
git log | Displays commit history | git log --oneline | Shows a list of commits with their IDs. |
git mv <old_name> <new_name> | Renames or moves a file | git mv old.txt new.txt | Helps rename a file while maintaining history. |
Branches allow developers to work on new features or bug fixes without affecting the main project.
Command | Description | Example | Explanation |
git branch | Lists, creates, or deletes branches | git branch feature-branch | Creates a new branch for a feature. |
git checkout <branch> | Switches between branches | git checkout feature-branch | Moves to a different branch. |
git switch <branch> | Alternative to git checkout | git switch main | A modern command to switch branches. |
git merge <branch> | Merges changes from one branch to another | git merge feature-branch | Combines changes from feature-branch into the current branch. |
git rebase <branch> | Reapplies changes from one branch onto another | git rebase main | Keeps commit history clean while merging. |
🚀 Example Workflow:
git checkout -b new-feature # Create a new branch
git add . # Stage changes
git commit -m "Added a new feature" # Commit changes
git checkout main # Switch to main branch
git merge new-feature # Merge changes into main
Command | Description | Example |
git diff | Shows changes between commits and working directory | git diff |
git log | Displays commit history | git log --graph --oneline --all |
git show | Displays details of a specific commit | git show HEAD |
git blame <file> | Shows who last modified each line of a file | git blame index.html |
git commit --amend | Modifies the last commit | git commit --amend -m "Updated message" |
Command | Description | Example |
git remote | Manages remote repositories | git remote add origin https://github.com/user/repo.git |
git fetch | Downloads objects from a remote repository | git fetch origin |
git pull | Fetches and merges changes from remote | git pull origin main |
git push | Uploads local commits to a remote repository | git push origin main |
Command | Description | Example |
git restore <file> | Discards changes in working directory | git restore index.html |
git reset --soft HEAD~1 | Undo last commit but keep changes staged | git reset --soft HEAD~1 |
git reset --mixed HEAD~1 | Undo last commit and unstage changes | git reset --mixed HEAD~1 |
git reset --hard HEAD~1 | Undo last commit and delete changes | git reset --hard HEAD~1 |
Command | Description |
pre-commit | Hook that runs before commit |
post-commit | Hook that runs after commit |
pre-push | Hook that runs before push |
Command | Description | Example |
git submodule add <repo_url> | Adds a submodule to a repo | git submodule add https://github.com/user/lib.git |
git submodule update --init --recursive | Updates submodules recursively | git submodule update --init --recursive |
git submodule foreach git pull | Updates all submodules | git submodule foreach git pull origin main |
git merge --no-ff <branch>
→ Merges a branch while preserving its history.git rebase --onto <new-base> <old-base> <branch>
→ Moves a branch onto a new base.git restore <file>
→ Discards changes in the working directory.git restore --staged <file>
→ Unstages a file without removing changes.git reset --soft HEAD~1
→ Removes the last commit but keeps changes staged.git reflog
→ Shows a log of all HEAD movements (useful for recovering lost commits).git bisect
→ Finds a faulty commit by binary search.git fsck
→ Checks the integrity of Git objects and repository.git remote -v
→ Displays remote repositories with their URLs.git remote rm <name>
→ Removes a remote repository reference.git submodule add <repo_url>
→ Adds a submodule (useful for linking repositories).git submodule update --init --recursive
→ Initializes and updates submodules.git config --global user.name "Your Name"
→ Sets global user name.git config --global user.email "your.email@example.com"
→ Sets global user email.git gc
→ Cleans up unnecessary files to optimize repository performance.git shortlog -sn
→ Displays commit statistics by author.git archive --format=zip HEAD > latest.zip
→ Exports the latest version of the repository as a ZIP file.git checkout correct-branch
git cherry-pick <commit-hash>
git reset --hard HEAD~1
git reset --hard HEAD~1
git push --force origin main
git status
git mergetool
git commit -m "Resolved conflicts"
We are Recommending you:
Step Out of Your Comfort Zone: 10 Powerful...
Is Mobile Reels Harming Our Children? Here's...
Simple body language tricks1. Stand with...
Best Free Websites to Learn CodingIf you...
Git is a distributed version control system...
#...
Step Out of Your Comfort Zone: 10 Powerful...
these are following steps to remove...
Whenever you visit a website or access any...